﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;

public partial class RecoveryPWD : System.Web.UI.Page
{
    ServerObject mServerObject = null;

    protected override void InitializeCulture()
    {
        System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(HttpContext.Current.Request.UserLanguages[0]);
        System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(HttpContext.Current.Request.UserLanguages[0]);

        base.InitializeCulture();
    }

    private bool CheckServerObject()
    {
        bool result = false;

        if (Session["ServerModule"] != null)
        {
            mServerObject = (ServerObject)Session["ServerModule"];
        }

        if (mServerObject != null)
        {
            result = true;
        }

        return result;
    }

    private void ShowPanel(string panelName)
    {
        #region Hide panels

        resetPasswordStep1Panel.Visible = false;
        resetPasswordStep2Panel.Visible = false;

        #endregion Hide panels

        try
        {
            if (CheckServerObject())
            {
                #region Get Panel Name

                switch (panelName)
                {
                    #region Services
                    case "resetPasswordStep1Panel":
                        resetPasswordStep1Panel.Visible = true;
                       
                        break;

                    case "resetPasswordStep2Panel":
                        resetPasswordStep2Panel.Visible = true;
                       
                        break;                  

                    #endregion Services

                    default:
                        break;
                }

                #endregion Get Panel Name
            }
        }
        catch (Exception exception)
        {
            mServerObject.LogFatal(exception.Message);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["action"] != null)
        {
            string action = Request["action"].ToString();

            switch (action)
            {
                case "logout":
                    Session["UserData"] = null;
                    Session["ServerModule"] = null;
                    Response.Redirect("Default.aspx", true);
                    break;
                default:
                    break;
            }
        }
        
        ShowPanel(resetPasswordStep1Panel.ID);
    }

    public void ServerValidation(object source, ServerValidateEventArgs args)
    {
        try
        {
            string imputEmail = args.Value.ToString();
            DataTable dt = mServerObject.ChekExistRegistredEmail(imputEmail);

            if (dt != null && dt.Rows.Count > 0)
            {
                args.IsValid = true;
            }
            else
                args.IsValid = false;
        }

        catch (Exception ex)
        {
            args.IsValid = false;
        }
    }

    protected void resetPasswordStep1_OkButton_Click(object sender, EventArgs e)
    {
        if (resetPasswordStep1CustomValidator.IsValid)
        {
            string emailString = emailTextBox.Text;
            string newPassword = mServerObject.CreateRandomPassword(8);
            int solicitantID = mServerObject.GetSolicitantIdByEmail(emailString);

            if (mServerObject.UpdateSolicitantPassword(solicitantID, newPassword))
            {    
                #region sendEmail

                string emailTemplate = @"Buna ziua, <br/> "
                    + "Adersa aceasta de Email a fost indicata pentru efectuarea schimbarii parolei.  <br/>"
                    + " Pentru a putea intra pe pagina PROGRAMARII folositi urmatoarele date: <br />"
                    + " Nume utilizator: " + emailString + " <br />"
                    + " Parola: " + newPassword + " <br />"
                    + " Atentie! Dupa ce intrati pe pagina WEB folosind parola data - schimbati parola!";
                string emailSubject = "Mesaj de confirmare pentru o programare!";
                string resultEmail = Utils.EmailSend(emailString, emailTemplate, emailSubject);

                #endregion send email

                ShowPanel(resetPasswordStep2Panel.ID);
            }
            else
            {
                mServerObject.InfoText(this, "In momentul dat nu este posibil de a schimba parola. Incercati mai tirziu!");
            }
        }   
    }
}